var pageSize = 10;
var pageNum = 1;
var page;
var laypage;


$(function () {
    $("#menuId").val(sessionStorage.menuId);
    layui.use(['laydate', 'element', 'laypage', 'layer'], function () {
        $ = layui.jquery;//jquery
        laydate = layui.laydate;//日期插件
        element = layui.element();//面包导航
        laypage = layui.laypage;//分页
        layer = layui.layer;//弹出层

        //以上模块根据需要引入
    });
    setTimeout(function () {
        findUserDetail();
    }, 200);

});


//查看明细
function findUserDetail() {
    var menuId = $("#menuId").val();
    var userId = sessionStorage.userId;
    var params = {
        "type": "query",
        "menuId": menuId,
        "userId": userId,
        "pageSize": pageSize,
        "pageNum": pageNum
    };
    $.ajax({
        type: "post",
        url: "/user/invite/search",
        data: params,
        dataType: "json",
        success: function (data) {
            if (data !== null) {
                if (data.status != 1) {
                    alert(data.msg);
                    return false;
                } else {
                    page = data.page;
                    inviteLoad(data);
                }
                laypage({
                    cont: 'page'
                    , pages: page.pages
                    , first: 1
                    , curr: pageNum
                    , last: page.pages
                    , prev: '<em><</em>'
                    , next: '<em>></em>'
                    , jump: function (obj, first) {
                        pageNum = obj.curr;
                        if (!first) {
                            findUserDetail();
                        }
                    }
                });
            }
            if (data == 9999 && data != undefined) {
                window.location.href = "/login_toLogin";
            } else if (data == 1111 && data != undefined) {
                alert("无权操作");
            } else if (data.code = 2 && data.code != undefined) {
                $("#inviteAll").html("");
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log("error=" + errorThrown);
        }
    });
}

function inviteLoad(data) {
    $("#dataTotal").html("请稍后...");
    $("#inviteAll").html("");
    var inviteAllHtml = "";
    if (data.ivme != null) {
        var inviteMeHtml = teacherTr(data.ivme);
        inviteAllHtml += '<tr><td colspan="8">邀请我的人</td></tr>';
        inviteAllHtml += inviteMeHtml;
    }

    if (data.list.length > 0) {
        inviteAllHtml += '<tr><td colspan="8">我邀请的人</td></tr>';
    }

    for (var i = 0; i < data.list.length; i++) {
        inviteAllHtml += teacherTr(data.list[i]);
    }
    $("#inviteAll").html(inviteAllHtml);
    $("#dataTotal").html("共邀请：" + page.total + "人");
}

function teacherTr(data) {
    var html = "";
    var state = "正常";
    if (data.state == -1) {
        state = "封停";
    }
    if (data.state == 1) {
        state = "正常";
    }

    var sex;
    switch (data.sex) {
        case 0:
            sex = '男';
            break;
        case 1:
            sex = '女';
            break;
        case 2:
            sex = '不限';
            break;
        /* case 3:
             sex = "未设置"
             break;*/
        default:
            sex = "";
    }

    var role = "";
    if (data.role == 'USER') {
        role = '普通用户';
    }
    if (data.role == 'SUPER') {
        role = '超管';
    }
    if (data.role == 'CHUANG') {
        role = '创客';
    }
    if (data.role == 'HEHUOREN') {
        role = '合伙人';
    }

    html += '<tr class="odd">' +
        /*'<td class="center  sorting_1">'+
        '</td>'+*/
        '<td class=" ">' + data.userId + '</td>' +
        '<td class=" ">' + (data.nickname != null ? data.nickname : "") + '</td>' +
        '<td class=" ">' + sex + '</td>' +
        '<td class=" ">' + (!!data.birthday ? new Date(data.birthday).Format("yyyy-MM-dd") : "") + '</td>' +
        '<td class=" ">' + (data.phone != null ? data.phone : "") + '</td>' +
        '<td class=" ">' + data.cashBalance + '</td>' +
        '<td class=" ">' + data.awardCashBalance + '</td>' +
        '<td class=" ">' + data.taskAwardBalance + '</td>' +
        '<td class=" ">' + data.advertCoin + '</td>' +
        '<td class=" ">' + role + '</td>' +
        '<td class=" " style="color:red">' + state + '</td>' +
        '<td class=" ">' + (!!data.createTime ? new Date(data.createTime).Format("yyyy-MM-dd hh:mm:ss") : "") + '</td>';
    html += '</tr>';
    return html;
}